using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DDF.Data;
using DDF.Data.Providers;
using System.Data;
using System.Linq.Expressions;
using System.Configuration;
using ARR.Entities.Gateways;

namespace ARR.DataModel.Repositories
{
	public static partial class GeneralCommentsRepository
	{
		private static string connString = ConfigurationManager.ConnectionStrings["Default"].ConnectionString;
		
		public class GeneralCommentsDbo : DatabaseObject
        {
            public GeneralCommentsDbo()
            {
                Name = "GeneralComments";
                GeneratedClassName = "GeneralComments";
                SequenceName = "N/A";
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "Id", ColumnType = "int", IsIdentity = true, IsNullable = false, Length = 4, Precision = 10, Scale = 0, GeneratedPropertyName = "Id" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "RangeReviewId", ColumnType = "int", IsIdentity = false, IsNullable = false, Length = 4, Precision = 10, Scale = 0, GeneratedPropertyName = "RangeReviewId" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "ReportType", ColumnType = "varchar", IsIdentity = false, IsNullable = false, Length = 50, Precision = 0, Scale = 0, GeneratedPropertyName = "ReportType" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "Page", ColumnType = "int", IsIdentity = false, IsNullable = false, Length = 4, Precision = 10, Scale = 0, GeneratedPropertyName = "Page" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "PageTitle", ColumnType = "varchar", IsIdentity = false, IsNullable = true, Length = 50, Precision = 0, Scale = 0, GeneratedPropertyName = "PageTitle" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "PageSubtitle1", ColumnType = "varchar", IsIdentity = false, IsNullable = true, Length = 100, Precision = 0, Scale = 0, GeneratedPropertyName = "PageSubtitle1" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "PageSubtitle2", ColumnType = "varchar", IsIdentity = false, IsNullable = true, Length = 100, Precision = 0, Scale = 0, GeneratedPropertyName = "PageSubtitle2" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "Section", ColumnType = "varchar", IsIdentity = false, IsNullable = false, Length = 50, Precision = 0, Scale = 0, GeneratedPropertyName = "Section" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "Comment", ColumnType = "varchar", IsIdentity = false, IsNullable = false, Length = -1, Precision = 0, Scale = 0, GeneratedPropertyName = "Comment" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "Pdf", ColumnType = "varbinary", IsIdentity = false, IsNullable = false, Length = -1, Precision = 0, Scale = 0, GeneratedPropertyName = "Pdf" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "CategoryId", ColumnType = "int", IsIdentity = false, IsNullable = true, Length = 4, Precision = 10, Scale = 0, GeneratedPropertyName = "CategoryId" });
              
            }
        }
		
		public static List<GeneralCommentRow> Select(Expression<Func<GeneralCommentRow, bool>> expression)
        {
            List<GeneralCommentRow> rows = new List<GeneralCommentRow>();

            using (Database database = new SqlDataProvider(connString, "{0}Cursor", "dbo"))
            {
                IDataReader reader = database.Select<GeneralCommentRow>(new GeneralCommentsDbo(), expression);

                Load(reader, rows);
            }

            return rows;
        }
		
		public static List<GeneralCommentRow> Select()
		{
			List<GeneralCommentRow> rows = new List<GeneralCommentRow>();

			using (Database database = new SqlDataProvider(connString, "{0}Cursor", "dbo"))
			{
				IDataReader reader = database.Select<GeneralCommentRow>(new GeneralCommentsDbo(), null);

				Load(reader, rows);
			}

			return rows;
		}
		
		public static List<GeneralCommentRow> PagedSelect(Expression<Func<GeneralCommentRow, bool>> filterExpression, Expression<Func<GeneralCommentRow, object>> orderByExpression, int pageNumber, int pageSize)
		{
			List<GeneralCommentRow> rows = new List<GeneralCommentRow>();
			
			using (Database database = new SqlDataProvider(connString, "{0}Cursor", "dbo"))
			{
				IDataReader reader = database.PagedSelect<GeneralCommentRow>(new GeneralCommentsDbo(), filterExpression, orderByExpression, pageNumber, pageSize);

				Load(reader, rows);
			}

			return rows;
		}
		
		private static void Load(IDataReader reader, List<GeneralCommentRow> GeneralCommentList)
        {
            GeneralCommentRow row;

            while (reader.Read())
            {
                row = new GeneralCommentRow()
                {
					Id = Util.CastDbValue<int>(reader["Id"]),
					RangeReviewId = Util.CastDbValue<int>(reader["RangeReviewId"]),
					ReportType = Util.CastDbValue<string>(reader["ReportType"]),
					Page = Util.CastDbValue<int>(reader["Page"]),
					PageTitle = Util.CastDbValue<string>(reader["PageTitle"]),
					PageSubtitle1 = Util.CastDbValue<string>(reader["PageSubtitle1"]),
					PageSubtitle2 = Util.CastDbValue<string>(reader["PageSubtitle2"]),
					Section = Util.CastDbValue<string>(reader["Section"]),
					Comment = Util.CastDbValue<string>(reader["Comment"]),
					Pdf = Util.CastDbValue<byte[]>(reader["Pdf"]),
					CategoryId = Util.CastDbValue<int?>(reader["CategoryId"])
                };
                                
                GeneralCommentList.Add(row);
            }

            reader.Close();
        }

			
		public static void Insert(GeneralCommentRow row)
        {
            using (Database database = new SqlDataProvider(connString, "{0}Cursor", "dbo"))
            {
                database.Insert<GeneralCommentRow>(new GeneralCommentsDbo(), row);
            }
        }

		public static void Update(GeneralCommentRow row)
        {
            using (Database database = new SqlDataProvider(connString, "{0}Cursor", "dbo"))
            {
                database.Update<GeneralCommentRow>(new GeneralCommentsDbo(), row);
            }
        }

		public static void Delete(Expression<Func<GeneralCommentRow, bool>> expression)
        {
            using (Database database = new SqlDataProvider(connString, "{0}Cursor", "dbo"))
            {
                database.Delete<GeneralCommentRow>(new GeneralCommentsDbo(), expression);
            }
        }

	}
}
	
